package firstUniqChar;
class Solution {
    public char firstUniqChar(String s) {
        int[][] map = new int[26][2];
        for (int i = 0; i < s.length(); i++) {
            map[s.charAt(i)-'a'][0]++;
            if(map[s.charAt(i)-'a'][0] == 1){
                map[s.charAt(i)-'a'][1] = i;
            }
        }
        // 找到所有只出现一次的字符
        char ans = ' ';
        int pos = -1;
        for (int i = 0; i < 26; i++) {
            if(map[i][0] == 1) {
                if(ans == ' ') {
                    ans = (char) (i+'a');
                    pos = map[i][1];
                } else {
                    if(pos>map[i][1]) {
                        ans = (char) (i+'a');
                        pos = map[i][1];
                    }
                }
            }
        }
        return ans;
    }
}